<!DOCTYPE html>
<html >

<head>

  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Chapter 2 不一样的分面 | ggplot2忍者秘笈</title>
  <meta name="description" content="Chapter 2 不一样的分面 | ggplot2忍者秘笈">
  <meta name="generator" content="bookdown  and GitBook 2.6.7">

  <meta property="og:title" content="Chapter 2 不一样的分面 | ggplot2忍者秘笈" />
  <meta property="og:type" content="book" />
  
  
  
  

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="Chapter 2 不一样的分面 | ggplot2忍者秘笈" />
  
  
  

<meta name="author" content="余光创">



  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  
  
<link rel="prev" href="section.html">
<link rel="next" href="chapter-summary.html">
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />









<style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
  { position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
  { content: attr(title);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; pointer-events: all; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {  }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>Preface</a></li>
<li class="chapter" data-level="1" data-path="section.html"><a href="section.html"><i class="fa fa-check"></i><b>1</b> 简介</a></li>
<li class="chapter" data-level="2" data-path="chapter-facet.html"><a href="chapter-facet.html"><i class="fa fa-check"></i><b>2</b> 不一样的分面</a><ul>
<li class="chapter" data-level="2.1" data-path="chapter-facet.html"><a href="chapter-facet.html#section-1"><i class="fa fa-check"></i><b>2.1</b> 背景数据</a></li>
<li class="chapter" data-level="2.2" data-path="chapter-facet.html"><a href="chapter-facet.html#section-2"><i class="fa fa-check"></i><b>2.2</b> 特定的分面</a></li>
<li class="chapter" data-level="2.3" data-path="chapter-facet.html"><a href="chapter-facet.html#extra-facet"><i class="fa fa-check"></i><b>2.3</b> 额外的分面</a></li>
<li class="chapter" data-level="2.4" data-path="chapter-facet.html"><a href="chapter-facet.html#section-3"><i class="fa fa-check"></i><b>2.4</b> 坐标轴截断</a></li>
<li class="chapter" data-level="2.5" data-path="chapter-facet.html"><a href="chapter-facet.html#section-4"><i class="fa fa-check"></i><b>2.5</b> 不想干的分面</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="chapter-summary.html"><a href="chapter-summary.html"><i class="fa fa-check"></i><b>3</b> 统计量</a></li>
<li class="appendix"><span><b>Appendix</b></span></li>
<li class="chapter" data-level="" data-path="references.html"><a href="references.html"><i class="fa fa-check"></i>References</a></li>
</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">ggplot2忍者秘笈</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="chapter-facet" class="section level1">
<h1><span class="header-section-number">Chapter 2</span> 不一样的分面</h1>
<p>分面（facet）是将数据分解成多个子集，对各个子集分别进行作图，并联合进行展示。比如我们所熟知的数据<code>iris</code>和<code>mtcars</code>中，<code>iris</code>中有三个物种，而<code>mtcars</code>中的车有4、6和8个轮子的。我们可以按照这些分类变量，对数据进行切割作图。比如：</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="kw">ggplot</span>(mtcars, <span class="kw">aes</span>(mpg, disp)) <span class="op">+</span><span class="st"> </span>
<span class="st">    </span><span class="kw">geom_point</span>() <span class="op">+</span><span class="st"> </span><span class="kw">facet_grid</span>(. <span class="op">~</span><span class="st"> </span>cyl)</code></pre>
<p><img src="ggplot2-ninja_files/figure-html/unnamed-chunk-1-1.png" width="672" /></p>
<p><code>ggplot2</code>提供了<code>facet_grid()</code>和<code>facet_wrap()</code>两个函数，来实现分面可视化。上面的示例向我们展示了分面的用法，它提供给我们以纵向或横向的形式比较不同的类别，这里比较一个变量<code>cyl</code>（轮子数）中的三个元素，这是分面最常用的用法，同一变量中不同的取值，对数据进行切割，以横向或纵向比较，比如不同物种中某个相同的特征进行比较，按照月份切割可视化一年的数据，机器学习中训练集和测试集的比较。</p>
<div id="section-1" class="section level2">
<h2><span class="header-section-number">2.1</span> 背景数据</h2>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(mtcars, <span class="kw">aes</span>(mpg, disp)) <span class="op">+</span><span class="st"> </span>
<span class="st">    </span><span class="kw">geom_point</span>(<span class="dt">data =</span> mtcars[, <span class="dv">-2</span>], <span class="dt">color =</span> <span class="st">&#39;grey&#39;</span>) <span class="op">+</span><span class="st"> </span>
<span class="st">    </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">color =</span> <span class="kw">factor</span>(cyl))) <span class="op">+</span><span class="st"> </span><span class="kw">facet_grid</span>(. <span class="op">~</span><span class="st"> </span>cyl)</code></pre>
<p><img src="ggplot2-ninja_files/figure-html/unnamed-chunk-2-1.png" width="672" /></p>
</div>
<div id="section-2" class="section level2">
<h2><span class="header-section-number">2.2</span> 特定的分面</h2>
<pre class="sourceCode r"><code class="sourceCode r">d &lt;-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">mpg=</span><span class="dv">20</span>, <span class="dt">disp=</span><span class="dv">280</span>, <span class="dt">cyl=</span><span class="dv">6</span>, <span class="dt">label=</span><span class="st">&quot;额外的数据&quot;</span>)

<span class="kw">ggplot</span>(mtcars, <span class="kw">aes</span>(mpg, disp)) <span class="op">+</span><span class="st"> </span>
<span class="st">    </span><span class="kw">geom_point</span>(<span class="dt">data =</span> mtcars[, <span class="dv">-2</span>], <span class="dt">color =</span> <span class="st">&#39;grey&#39;</span>) <span class="op">+</span><span class="st"> </span>
<span class="st">    </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">color =</span> <span class="kw">factor</span>(cyl))) <span class="op">+</span><span class="st"> </span><span class="kw">facet_grid</span>(. <span class="op">~</span><span class="st"> </span>cyl) <span class="op">+</span>
<span class="st">    </span><span class="kw">geom_point</span>(<span class="dt">data =</span> d, <span class="dt">shape =</span> <span class="dv">2</span>, <span class="dt">size=</span><span class="dv">10</span>, <span class="dt">color =</span> <span class="st">&#39;red&#39;</span>) <span class="op">+</span>
<span class="st">    </span><span class="kw">geom_text</span>(<span class="kw">aes</span>(<span class="dt">label=</span>label), <span class="dt">data =</span> d, <span class="dt">size=</span><span class="dv">5</span>)</code></pre>
<p><img src="ggplot2-ninja_files/figure-html/unnamed-chunk-3-1.png" width="672" /></p>
</div>
<div id="extra-facet" class="section level2">
<h2><span class="header-section-number">2.3</span> 额外的分面</h2>
<pre class="sourceCode r"><code class="sourceCode r">d &lt;-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">mpg=</span><span class="kw">sample</span>(mtcars<span class="op">$</span>mpg, <span class="dv">20</span>), 
                <span class="dt">disp=</span><span class="kw">sample</span>(mtcars<span class="op">$</span>disp, <span class="dv">20</span>), 
                <span class="dt">cyl=</span><span class="dv">2</span>)

<span class="kw">ggplot</span>(mtcars, <span class="kw">aes</span>(mpg, disp)) <span class="op">+</span><span class="st"> </span>
<span class="st">    </span><span class="kw">geom_point</span>(<span class="dt">data =</span> mtcars[, <span class="dv">-2</span>], <span class="dt">color =</span> <span class="st">&#39;grey&#39;</span>) <span class="op">+</span><span class="st"> </span>
<span class="st">    </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">color =</span> <span class="kw">factor</span>(cyl))) <span class="op">+</span><span class="st"> </span><span class="kw">facet_grid</span>(. <span class="op">~</span><span class="st"> </span>cyl) <span class="op">+</span>
<span class="st">    </span><span class="kw">geom_point</span>(<span class="dt">data =</span> d)</code></pre>
<p><img src="ggplot2-ninja_files/figure-html/unnamed-chunk-4-1.png" width="672" /></p>
<p>这样就加上额外的分面了，当然有一个问题是额外加的分面没有上色，这个解决方案就是先不加色，而在后面再映射颜色。</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(mtcars, <span class="kw">aes</span>(mpg, disp)) <span class="op">+</span><span class="st"> </span>
<span class="st">    </span><span class="kw">geom_point</span>(<span class="dt">data =</span> mtcars[, <span class="dv">-2</span>], <span class="dt">color =</span> <span class="st">&#39;grey&#39;</span>) <span class="op">+</span><span class="st"> </span>
<span class="st">    </span><span class="kw">geom_point</span>() <span class="op">+</span><span class="st"> </span><span class="kw">facet_grid</span>(. <span class="op">~</span><span class="st"> </span>cyl) <span class="op">+</span>
<span class="st">    </span><span class="kw">geom_point</span>(<span class="dt">data =</span> d) <span class="op">+</span><span class="st"> </span><span class="kw">aes</span>(<span class="dt">color =</span> <span class="kw">factor</span>(cyl))</code></pre>
<p><img src="ggplot2-ninja_files/figure-html/unnamed-chunk-5-1.png" width="672" /></p>
</div>
<div id="section-3" class="section level2">
<h2><span class="header-section-number">2.4</span> 坐标轴截断</h2>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">set.seed</span>(<span class="dv">2019-01-19</span>)
d &lt;-<span class="st"> </span><span class="kw">data.frame</span>(
    <span class="dt">x =</span> <span class="dv">1</span><span class="op">:</span><span class="dv">20</span>, 
    <span class="dt">y =</span> <span class="kw">c</span>(<span class="kw">rnorm</span>(<span class="dv">5</span>) <span class="op">+</span><span class="st"> </span><span class="dv">4</span>, <span class="kw">rnorm</span>(<span class="dv">5</span>) <span class="op">+</span><span class="st"> </span><span class="dv">20</span>, <span class="kw">rnorm</span>(<span class="dv">5</span>) <span class="op">+</span><span class="st"> </span><span class="dv">5</span>, <span class="kw">rnorm</span>(<span class="dv">5</span>) <span class="op">+</span><span class="st"> </span><span class="dv">22</span>)
)

<span class="kw">ggplot</span>(d, <span class="kw">aes</span>(x, y)) <span class="op">+</span><span class="st"> </span><span class="kw">geom_col</span>()</code></pre>
<p><img src="ggplot2-ninja_files/figure-html/unnamed-chunk-6-1.png" width="672" /></p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(dplyr)

breaks =<span class="st"> </span><span class="kw">c</span>(<span class="dv">7</span>, <span class="dv">17</span>)
d<span class="op">$</span>.type &lt;-<span class="st"> </span><span class="ot">NA</span>
d<span class="op">$</span>.type[d<span class="op">$</span>y <span class="op">&lt;</span><span class="st"> </span>breaks[<span class="dv">1</span>]] =<span class="st"> &quot;small&quot;</span>
d<span class="op">$</span>.type[d<span class="op">$</span>y <span class="op">&gt;</span><span class="st"> </span>breaks[<span class="dv">2</span>]] =<span class="st"> &quot;big&quot;</span>

d &lt;-<span class="st"> </span><span class="kw">filter</span>(d, .type <span class="op">==</span><span class="st"> &#39;big&#39;</span>) <span class="op">%&gt;%</span><span class="st"> </span>
<span class="st">    </span><span class="kw">mutate</span>(<span class="dt">.type =</span> <span class="st">&quot;small&quot;</span>, <span class="dt">y =</span> breaks[<span class="dv">1</span>]) <span class="op">%&gt;%</span><span class="st"> </span>
<span class="st">    </span><span class="kw">bind_rows</span>(d)

mymin =<span class="st"> </span><span class="cf">function</span>(y) <span class="kw">ifelse</span>(y <span class="op">&lt;=</span><span class="st"> </span>breaks[<span class="dv">1</span>], <span class="dv">0</span>, breaks[<span class="dv">2</span>])               
p &lt;-<span class="st"> </span><span class="kw">ggplot</span>(d, <span class="kw">aes</span>(x, y)) <span class="op">+</span><span class="st"> </span>
<span class="st">    </span><span class="kw">geom_rect</span>(<span class="kw">aes</span>(<span class="dt">xmin =</span> x <span class="op">-</span><span class="st"> </span><span class="fl">.4</span>, <span class="dt">xmax =</span> x <span class="op">+</span><span class="st"> </span><span class="fl">.4</span>, <span class="dt">ymin =</span> <span class="kw">mymin</span>(y), <span class="dt">ymax =</span> y)) <span class="op">+</span>
<span class="st">    </span><span class="kw">facet_grid</span>(.type <span class="op">~</span><span class="st"> </span>., <span class="dt">scales =</span> <span class="st">&quot;free&quot;</span>) <span class="op">+</span><span class="st"> </span>
<span class="st">    </span><span class="kw">theme</span>(<span class="dt">strip.text=</span><span class="kw">element_blank</span>())
p</code></pre>
<p><img src="ggplot2-ninja_files/figure-html/unnamed-chunk-7-1.png" width="672" /></p>
</div>
<div id="section-4" class="section level2">
<h2><span class="header-section-number">2.5</span> 不想干的分面</h2>
<p>在<a href="chapter-facet.html#extra-facet">额外的分面</a>一节中，应该看出一点端倪。</p>
<p>如ggtree中的facet_plot</p>

</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="section.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="chapter-summary.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"google": false,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["facebook", "google", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"history": {
"link": null,
"text": null
},
"download": null,
"toc": {
"collapse": "subsection"
}
});
});
</script>

</body>

</html>
